function Message(id){
	this.id = id;

	this.user;
	this.text;
	this.time;
	this.parent;
	this.parentRepliesNumber;
	this.dom = {};

	if(isNumeric(id)) global.addObjectItem('message',this,this.getKey());
}

Message.prototype.getKey = function(){
	return 'message_'+this.id;
}

Message.prototype.assignProperties = function(user, text, time, parent, parentRepliesNumber){
	if(isNumeric(user)) user = global.getObject('user', 'user_'+user);
	if(parent) parent = global.getObject('message','message_'+parent);		
	if(isString(time)) time = getDate(time);

	this.user = user;
	this.time = time;
	this.text = text;
	this.parent = parent;
	this.parentRepliesNumber = parentRepliesNumber;

	global.getObject('user', 'user_'+user.id).lastActivity = this.time;
	global.getObject('user', 'user_'+user.id).messageCounter++;
}

Message.prototype.place = function(){
	var messagesElement = global.getElement('messages');

	messagesElement.appendChild(this.createMessageItem());

	messagesElement.scrollTop = messagesElement.scrollHeight;
}

Message.prototype.setId = function(id){
	this.id = id;
	
	if(isNumeric(id)) global.addObjectItem('message',this,this.getKey());

	setData(this.dom.element, 'id', id);
}

Message.prototype.createMessagesGroup = function(){
	var messagesGroup = document.createElement('DIV');
	messagesGroup.className = 'messagesGroup';

	//setData(messagesGroup, 'user', this.group.getKey());
	global.addElement(messagesGroup,'messagesGroup');
	global.createObject('lastMessageFrom', this.user.id);

	//this.dom.group = messagesGroup;

	return messagesGroup;
}
/*
Message.prototype.getElement = function(){
	if(this.dom && 'element' in this.dom) return this.copyElement();
	else{
		this.dom = new Array();
		var element = this.createMessageItem();
		this.dom['element'] = element;

		return element;
	}
}*/

Message.prototype.createMessageItem = function(){
	if(this.dom.element) return this.copyElement(); //testar se eh o caso do reply vazio

	var message = document.createElement('DIV');
	message.className = 'message';

	var name = document.createElement('P');
	name.className = 'name';
	name.appendChild(document.createTextNode(this.user.name));

	var picture = document.createElement('DIV');
	picture.className = 'picture';
	var img = document.createElement('IMG');
	img.src = this.user.getPicturePath();
	picture.appendChild(img);

	var text = document.createElement('P');
	text.className = 'text';
	
	text.innerHTML = this.text;

	var timeText = (isString(this.time)) ? this.time : this.time.day+'/'+this.time.month+'/'+this.time.year+' '+this.time.hour+':'+this.time.minute+':'+this.time.second;

	var time = document.createElement('P');
	time.className = 'time';
	time.appendChild(document.createTextNode(timeText));

	//if(this.parent != null && this.parent != '') var parent = this.createParentElement();

	var reply = document.createElement('DIV');
	reply.className = 'reply';
	reply.innerHTML = 'Responder';
	setEvent(reply, 'click', replyMessage);

	setData(message, 'id', this.id);
	setData(message, 'user', this.user.id);

	var parent = null;
	if(this.parent != null && this.parent != ''){ // verificar se é o caso do reply vazio
		parent = this.createParentElement();
		message.appendChild(parent);
		this.dom.parent = parent;
	}	
	message.appendChild(picture);
	message.appendChild(name);
	message.appendChild(text);
	message.appendChild(reply);
	message.appendChild(time);

	setEvent(message,'mouseover',calculateRelativeTime);
	setEvent(message,'mouseout',mouseOutMessage);

	this.dom.element = message;
	this.dom.name = name;
	this.dom.time = time;

	return message;
}

Message.prototype.createParentElement = function(){
	if(isNumeric(this.parent)) this.parent = global.getObject('message','message_'+this.parent);

	var parent = document.createElement('DIV');
	parent.className = 'parent';

	var quote = document.createElement('P');
	quote.className = 'quote';

	var name = document.createElement('P');
	name.className = 'name';
	name.innerHTML = this.parent.dom.name.innerHTML;

	var parentText = this.parent.text;
	if(parentText.length > 37) parentText = this.parent.text.substr(0,37)+'...';

	quote.innerHTML = parentText; //take only the first 37 characters of the parent message text;

	var showTreeButton = document.createElement('DIV');
	showTreeButton.className = 'showTree button';
	showTreeButton.innerHTML = this.parentRepliesNumber;
	setEvent(showTreeButton, 'click', showReplyTree);

	parent.appendChild(name);
	parent.appendChild(quote);
	parent.appendChild(showTreeButton);

	return parent;
}

Message.prototype.copyElement = function(){
	return this.dom.element.cloneNode(true);
}

Message.prototype.setParentRepliesNumber = function(parentRepliesNumber){
	this.parentRepliesNumber = parentRepliesNumber;
}

Message.prototype.updateParentElement = function(){
	var parentRepliesNumber = this.parentRepliesNumber;
	var showTree = this.dom.parent.getElementsByClassName('showTree')[0];
	showTree.innerHTML = parentRepliesNumber;
	showTree.title = 'Esta conversa tem '+parentRepliesNumber+' mensagens';
}

var globalOverMessageId = 0;

function calculateRelativeTime(event){
	var now = new Date().getTime();
	var message = findParentWithClass(event.target,'message');

	var id = getData(message,'id');
	if(id == globalOverMessageId) return false;

	globalOverMessageId = id;
	var messageObj = global.getObject('message','message_'+id);

	var millisecondsDifference = now - messageObj.time.date.getTime();

	var secondsPast = Math.floor(millisecondsDifference/1000);

	var response = '';

	var hours = Math.floor(secondsPast/3600);
	var minutesBase = secondsPast%3600;
	var minutes = Math.floor(minutesBase / 60);    

	if(hours > 0){
		response = 'há cerca de '+hours+' hora';
		if(hours > 1) response += 's';
	}else{
		if(minutes > 0){
			response = 'há '+minutes+' minuto';
			if(minutes > 1) response += 's';
		}else{
			response = 'há '+secondsPast+' segundos';
		}
	}

	messageObj.dom.time.innerHTML = response;
}

function mouseOutMessage(event){
	var message = findParentWithClass(event.relatedTarget,'message');
	
	if(message){
		var id = getData(message,'id');
		if(id != globalOverMessageId) globalOverMessageId = 0;
	}else globalOverMessageId = 0;
}
